package com.github.ylt.utils;

import java.nio.charset.StandardCharsets;

public class ByteUtils {
    private ByteUtils() {}
    private static class SingletonHolder {
        private static final ByteUtils INSTANCE = new ByteUtils();
    }
    public static ByteUtils getInstance() {
        return SingletonHolder.INSTANCE;
    }

    /**
     * 主要为了去bytebuffer掉填充的数据
     * @param bytes byte数组
     * @return 返回实际内容并去掉填充的数据
     */
    public String bytesToString(byte[] bytes) {
        int length = 0;
        for (int i = 0; i < bytes.length; i++) {
            if(bytes[i] == 0){
                length = i;
                break;
            }
        }
        return new String(bytes, 0, length, StandardCharsets.UTF_8);
    }
}
